package com.tjec.project.businessWorkbench.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.common.constant.UserConstants;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.web.domain.TreeSelect;
import com.tjec.project.businessWorkbench.domain.BmsMenu;
import com.tjec.project.businessWorkbench.mapper.BmsMenuMapper;
import com.tjec.project.system.domain.BmsMdBusinessCategory;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.domain.vo.MetaVo;
import com.tjec.project.system.domain.vo.RouterVo;
import com.tjec.project.system.mapper.BmsMdBusinessCategoryMapper;
import com.tjec.project.system.mapper.SysRoleMenuMapper;
import com.tjec.project.businessWorkbench.service.IBmsMenuService;

/**
 * 菜单 业务层处理
 * 
 * @author tjec
 */
@Service
public class BmsMenuServiceImpl implements IBmsMenuService
{
    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    @Autowired
    private BmsMenuMapper menuMapper;
    
    @Autowired
    private BmsMdBusinessCategoryMapper bmsMdBusinessCategoryMapper;

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

   

    /**
     * 根据业务类别ID查询菜单
     * 
     * @param bmsId 业务类别Id
     * @return 菜单列表
     */
    @Override
    public List<BmsMenu> selectMenuTreeByBmsId(String bmsId)
    {
    	List<BmsMenu> res = new ArrayList<BmsMenu>();
    	BmsMdBusinessCategory bmsMdBusinessCategory = bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryByGuId(bmsId);
    	if(bmsMdBusinessCategory !=null && StringUtils.isNotBlank(bmsMdBusinessCategory.getMenuId())) {
    		res = menuMapper.selectMenuTreeByMenuId(bmsMdBusinessCategory.getMenuId(),bmsId);
    	}
    	return getChildPerms(res, "0");
    }

   
    @Override
    public List<RouterVo> buildMenus(List<BmsMenu> menus)
    {
        List<RouterVo> routers = new LinkedList<RouterVo>();
        for (BmsMenu menu : menus)
        {
            RouterVo router = new RouterVo();
            router.setName(StringUtils.capitalize(menu.getPath()));
            router.setPath(getRouterPath(menu));
            router.setComponent(StringUtils.isEmpty(menu.getComponent()) ? "Layout" : menu.getComponent());
            router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon()));
            router.setBusinessId(menu.getBusinessId());
            List<BmsMenu> cMenus = menu.getChildren();
            if (!cMenus.isEmpty() && cMenus.size() > 0 && "M".equals(menu.getMenuType()))
            {
                router.setAlwaysShow(true);
                router.setRedirect("noRedirect");
                router.setChildren(buildMenus(cMenus));
            }
            routers.add(router);
        }
        return routers;
    }

    /**
     * 获取路由地址
     * 
     * @param menu 菜单信息
     * @return 路由地址
     */
    public String getRouterPath(BmsMenu menu)
    {
        String routerPath = menu.getPath();
        // 非外链并且是一级目录
        if ("0".equals(menu.getParentId()) && "1".equals(menu.getIsFrame()))
        {
            routerPath = "/" + menu.getPath();
        }
        return routerPath;
    }

    /**
     * 根据父节点的ID获取所有子节点
     * 
     * @param list 分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<BmsMenu> getChildPerms(List<BmsMenu> list, String parentId)
    {
        List<BmsMenu> returnList = new ArrayList<BmsMenu>();
        for (Iterator<BmsMenu> iterator = list.iterator(); iterator.hasNext();)
        {
        	BmsMenu t = (BmsMenu) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (parentId.equals(t.getParentId()))
            {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     * 
     * @param list
     * @param t
     */
    private void recursionFn(List<BmsMenu> list, BmsMenu t)
    {
        // 得到子节点列表
        List<BmsMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BmsMenu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<BmsMenu> it = childList.iterator();
                while (it.hasNext())
                {
                    BmsMenu n = (BmsMenu) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BmsMenu> getChildList(List<BmsMenu> list, BmsMenu t)
    {
        List<BmsMenu> tlist = new ArrayList<BmsMenu>();
        Iterator<BmsMenu> it = list.iterator();
        while (it.hasNext())
        {
            BmsMenu n = (BmsMenu) it.next();
            if (t.getMenuId().equals(n.getParentId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BmsMenu> list, BmsMenu t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
